package controllers;

import java.util.ArrayList;
import java.util.List;

import models.Email;
import models.Idea;
import models.Topic;
import models.User;

import org.apache.commons.mail.EmailException;

/**
 * This thread is created once a idea is created, it allows the system to check
 * if the idea is inactive every 2 hrs and repeats this task for 10 times After
 * each check, the system sends an email and a notification to the owner to
 * either delete or add an comment to the topic,,Other wise after the last
 * check, the system will delete the idea
 * 
 * @author Nourhan El-Nehlawy
 * 
 */

public class SendTriggersIdea extends Thread {
	/**
	 * The idea that the system will check
	 * 
	 * @author Nourhan El-Nehalwy
	 */

	public Idea idea;

	/**
	 * Default constructor
	 * 
	 * @author Nourhan El-Nehalwy
	 */
	public SendTriggersIdea() {

	}

	/**
	 * Constructor that takes idea as a parameter
	 * 
	 * @param idea
	 *            the input that the constructor takes when the thread is
	 *            created
	 * @author Nourhan El-nehlawy
	 */
	public SendTriggersIdea(Idea idea) {
		this.idea = idea;

	}

	/**
	 * The run method which is executed once the thread is started, it calls
	 * another method which performs the task
	 * 
	 * @author Nourhan El-Nehlawy
	 */
	public void run() {
		try {

			runIdea(this.idea);

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * This method takes the idea as an input to perform the check by calling
	 * another method called sendTriggers which checks if the idea is active or
	 * not and responds accordingly
	 * 
	 * @param idea
	 *            the input of the method, which is the topic the system will
	 *            check
	 * @throws EmailException
	 *             an exception because of sending an email
	 * @throws InterruptedException
	 *             exception used in threads
	 * 
	 * @author Nourhan El-Nehlawy
	 */
	public void runIdea(Idea idea) throws EmailException, InterruptedException {
		while (true) {
			SendTriggersIdea.sendTriggers(idea);
			// Long id = (long) 1;
			// SystemOrg s = SystemOrg.findById(id);
			// SystemOrg s = SystemOrg.find("byEmail",
			// "Topsoft.system@gmail.com").first();

			// s.systemSendNotification("Please add ideas to your topic or delete it",
			// topic.author, "topic", topic.id);

			// this.sleep(1000*60*60*2)
			if (idea.notifications < 0) {
				// System.out.println("5alas");

				this.stop();
			}
			System.out.println("test1");
			this.sleep(1000 * 60 * 60 * 2);
			System.out.println("test2");
		}
	}

	/**
	 * This method checks if the idea is inactive, and sends a notification and
	 * an email to the user to either delete the idea or add a comment to it
	 * 
	 * @param id
	 *            the input idea
	 * @throws EmailException
	 *             exeption because of using the sendEmail
	 * 
	 * @author Nourhan El-Nehlawy
	 */
	public static void sendTriggers(Idea id) throws EmailException {

		if (id.hasComments == false && id.notifications > 0) {
			System.out.println("nooooooooooour");
			System.out.println(id.author.name);

			// ArrayList<User> user = new ArrayList<User>();
			// user.add(t.author);
			// t.author.sendNotificationWithType("Please add ideas to your topic or delete it",user
			// , "Topic", t.id);
			//

			String src = "Topsoft.system@gmail.com";
			String dest = id.author.email;
			String subject = "Inactive Topic";
			String msg = "Please add ideas to your topic or delete it";

			// Email x = new Email(src, dest, subject,msg);
			// x.sendEmail();

			System.out.println("mail is sent");
		}
		if (id.notifications == 0 && id.hasComments == false) {
			System.out.println("no ideas w not =0");
			System.out.println("nour");

			// delete topic
			// send mail and notification that the topic will be deleted
			// ArrayList<User>u = new ArrayList<User>();
			// u.add(t.author);
			// t.author.sendNotificationWithType("Please note that your Topic will be deleted",
			// u, "Topic",t.id );

			// String src = "Topsoft.system@gmail.com";
			// String dest =t.author.email;
			// System.out.println("dest");
			// String subject = "Inactive Topic";
			// String msg = "Please note that your Topic will be deleted";
			// Email x = new Email(src, dest, subject,msg);
			// x.sendEmail();

			System.out.println("mail is sent");

		}
		id.notifications--;
		// System.out.println(notifications);
	}

}